
//
//  SinaApiRequestUtil.m
//  Ridding
//
//  Created by zys on 12-3-21.
//  Copyright 2012年 __MyCompanyName__. All rights reserved.
//

#import "SinaApiRequestUtil.h"
static SinaApiRequestUtil *sinaApiRequestUtil=nil;
@implementation SinaApiRequestUtil
@synthesize staticInfo;
@synthesize checker;
- (id)init
{
    self = [super init];
    if (self) {
        staticInfo=[StaticInfo getSinglton];
        checker=[ResponseCodeCheck getSinglton];
    }
    return self;
}

+ (SinaApiRequestUtil*)getSinglton
{
    @synchronized(self) {
        if (sinaApiRequestUtil == nil) {
            sinaApiRequestUtil=[[self alloc] init]; // assignment not done here
        }
    }
    return sinaApiRequestUtil;
}

-(NSDictionary*)getUserInfo{
    NSString* methodName=[NSString stringWithFormat:@"https://%@/2/users/show.json?access_token=%@&uid=%@",apiSinaHost,staticInfo.user.accessToken,staticInfo.user.accessUserId];
    NSURL *apiUrl=[NSURL URLWithString:methodName];
    ASIHTTPRequest *arequest=[ASIHTTPRequest requestWithURL:apiUrl];
    [arequest setRequestMethod:@"GET"];
    [arequest startSynchronous];
    NSString* response= [arequest responseString];
    NSDictionary *responseDic=[response JSONValue];
   if([responseDic objectForKey:@"error_code"]){
       return nil;
    }
    return responseDic;
}

-(NSMutableArray*) getAtUserList:(NSString*)q type:(NSNumber*)type{
    q=[[NSString alloc]initWithUTF8String:[q UTF8String]];
    NSString* methodName=[NSString stringWithFormat:@"https://%@/2/search/suggestions/at_users.json?access_token=%@&q=%@&count=%@&type=%@&range=%@&uid=%@",apiSinaHost,staticInfo.user.accessToken,q,@"30",type,@"0",staticInfo.user.accessUserId];
    methodName= [[NSString alloc]initWithUTF8String:[methodName UTF8String]];
    NSURL *apiUrl=[NSURL URLWithString:methodName];
    ASIHTTPRequest *arequest=[ASIHTTPRequest requestWithURL:apiUrl];
    [arequest setRequestMethod:@"GET"];
    [arequest setDefaultResponseEncoding:NSUTF8StringEncoding];
    [arequest startSynchronous];
    NSString* response= [arequest responseString];
    id resp=[response JSONValue];
    if([resp isKindOfClass:[NSArray class]]){
        return resp;
    }
    return nil;

}

-(NSString*)checkTokenIsValid{
    NSString* methodName=[NSString stringWithFormat:@"https://%@/2/account/get_uid.json?access_token=%@",apiSinaHost,staticInfo.user.accessToken];
    methodName= [[NSString alloc]initWithUTF8String:[methodName UTF8String]];
    NSURL *apiUrl=[NSURL URLWithString:methodName];
    ASIHTTPRequest *arequest=[ASIHTTPRequest requestWithURL:apiUrl];
    [arequest setRequestMethod:@"GET"];
    [arequest setDefaultResponseEncoding:NSUTF8StringEncoding];
    [arequest startSynchronous];
    NSString* response= [arequest responseString];
    NSDictionary* responseDic=[response JSONValue];
    if([responseDic objectForKey:@"error_code"]){
        return nil;
    }
    return [NSString stringWithFormat:@"%@",[responseDic objectForKey:@"uid"]];
}


-(void)quit{
    NSString* methodName=[NSString stringWithFormat:@"https://%@/2/account/end_session.json?access_token=%@",apiSinaHost,staticInfo.user.accessToken];
    methodName= [[NSString alloc]initWithUTF8String:[methodName UTF8String]];
    NSURL *apiUrl=[NSURL URLWithString:methodName];
    ASIHTTPRequest *arequest=[ASIHTTPRequest requestWithURL:apiUrl];
    [arequest setRequestMethod:@"GET"];
    [arequest setDefaultResponseEncoding:NSUTF8StringEncoding];
    [arequest startAsynchronous];
}

-(void)friendShip:(NSString*)nickName accessUserId:(NSString*)accessUserId{
    NSString* methodName=[NSString stringWithFormat:@"https://%@/2/friendships/create.json?access_token=%@&uid=%@",apiSinaHost,staticInfo.user.accessToken,accessUserId];
    methodName= [[NSString alloc]initWithUTF8String:[methodName UTF8String]];
    NSURL *apiUrl=[NSURL URLWithString:methodName];
    ASIHTTPRequest *arequest=[ASIHTTPRequest requestWithURL:apiUrl];
    [arequest setRequestMethod:@"POST"];
    [arequest setDefaultResponseEncoding:NSUTF8StringEncoding];
    [arequest startSynchronous];
    //NSString *response=[arequest responseString];

}

//异步请求完成
- (void)requestFinished:(ASIHTTPRequest *)request
{
   // NSLog(@"123");  
}
//异步请求失败
- (void)requestFailed:(ASIHTTPRequest *)request
{
    //NSLog(@"223"); 
}
@end
